package com.yangyang.dingnote;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.coordinatorlayout.widget.CoordinatorLayout;

import com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton;

public class CustomFabBehavior extends CoordinatorLayout.Behavior<ExtendedFloatingActionButton> {

    public CustomFabBehavior(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean onStartNestedScroll(
            @NonNull CoordinatorLayout coordinatorLayout,
            @NonNull ExtendedFloatingActionButton child,
            @NonNull View directTargetChild,
            @NonNull View target,
            int axes,
            int type
    ) {
        return true; // 监听垂直滚动
    }

    @Override
    public void onNestedScroll(
            @NonNull CoordinatorLayout coordinatorLayout,
            @NonNull ExtendedFloatingActionButton child,
            @NonNull View target,
            int dxConsumed,
            int dyConsumed,
            int dxUnconsumed,
            int dyUnconsumed,
            int type
    ) {
        if (dyConsumed > 0) {
            // 下滑，隐藏按钮
            child.shrink();
        } else if (dyConsumed < 0) {
            // 上滑，显示按钮
            child.extend();
        }
    }
}